package dao;

import model.OrderDetail;
import util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单详情数据访问对象类，用于操作订单详情表数据
 */
public class OrderDetailDAO {
    /**
     * 添加订单详情
     * @param orderDetail 要添加的订单详情对象
     */
    public void addOrderDetail(OrderDetail orderDetail) {
        String sql = "INSERT INTO order_detail(order_id, product_id, quantity) VALUES(?,?,?)";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setInt(1, orderDetail.getOrderId());
            preparedStatement.setInt(2, orderDetail.getProductId());
            preparedStatement.setInt(3, orderDetail.getQuantity());

            int rowsAffected = preparedStatement.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("订单详情添加成功！");
            } else {
                System.out.println("订单详情添加失败！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据订单详情 ID 获取订单详情
     * @param orderDetailId 订单详情 ID
     * @return 对应的订单详情对象，若未找到则返回 null
     */
    public OrderDetail getOrderDetailById(int orderDetailId) {
        String sql = "SELECT * FROM order_detail WHERE order_detail_id = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setInt(1, orderDetailId);

            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOrderDetailId(resultSet.getInt("order_detail_id"));
                orderDetail.setOrderId(resultSet.getInt("order_id"));
                orderDetail.setProductId(resultSet.getInt("product_id"));
                orderDetail.setQuantity(resultSet.getInt("quantity"));

                return orderDetail;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 根据订单 ID 获取订单详情列表
     * @param orderId 订单 ID
     * @return 订单详情列表
     */
    public List<OrderDetail> getOrderDetailsByOrderId(int orderId) {
        List<OrderDetail> orderDetailList = new ArrayList<>();
        String sql = "SELECT * FROM order_detail WHERE order_id = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setInt(1, orderId);

            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOrderDetailId(resultSet.getInt("order_detail_id"));
                orderDetail.setOrderId(resultSet.getInt("order_id"));
                orderDetail.setProductId(resultSet.getInt("product_id"));
                orderDetail.setQuantity(resultSet.getInt("quantity"));

                orderDetailList.add(orderDetail);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return orderDetailList;
    }
}